package com.woniuxy.order.exception;

import com.woniuxy.order.entity.ResponseResult;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class RentHouseExsitException {
    @ExceptionHandler()
    @ResponseBody
    public ResponseResult methodArgumentNotValidException(MethodArgumentNotValidException e) {
        ResponseResult responseResult = new ResponseResult(102, "");
        BindingResult result = e.getBindingResult();
        if (result.hasErrors()) {
            HashMap<Object, String> map = new HashMap<>();
//            result.getAllErrors().forEach(c -> map.put(c.getArguments(), c.getDefaultMessage()));
            result.getAllErrors().forEach(c -> map.put(c.getArguments(), c.getDefaultMessage()));

            responseResult.setData(map);
        }
        return responseResult;
    }


}
